from sqlalchemy import Column, String, Text, Integer, PickleType, DateTime
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relation
from sqlalchemy.orm.collections import attribute_mapped_collection
from sc_console.model.meta import Base

class Heartbeat(Base):
    __tablename__ = 'heartbeats'

    beat_time = Column(DateTime, primary_key=True)

    responses = relation('HeartbeatResponse',
         collection_class=attribute_mapped_collection('app_name'))
    
    def __init__(self, beat_time):
        self.beat_time = beat_time

    def add_response(self, app_name, response_time):
        response = HeartbeatResponse(app_name, response_time)
        self.responses[app_name] = response

class HeartbeatResponse(Base):
    __tablename__ = 'heartbeat_responses'

    id = Column(Integer, primary_key=True)
    app_name = Column(String(50))
    response_time = Column(DateTime)

    heartbeat_id = Column(DateTime, ForeignKey('heartbeats.beat_time'))
    
    def __init__(self, app_name, response_time):
        self.app_name = app_name
        self.response_time = response_time
    
class ErrorReport(Base):
    __tablename__ = 'error_reports'

    id = Column(Integer, primary_key=True)
    time = Column(DateTime)

    app_name = Column(String(100))
    error_message = Column(String(200))
    message_name = Column(String(100))
    message_data = Column(PickleType)
    traceback = Column(PickleType)
    log = Column(PickleType)

class YouTubePartialParse(Base):
    __tablename__ = 'youtube_partial_parses'

    time = Column(DateTime)
    video_id = Column(String(11), primary_key=True)
    title = Column(String(200))

    date_played = Column(PickleType)
    teams = Column(PickleType)
    players = Column(PickleType)
    game_number = Column(PickleType)

    possible_results = Column(PickleType)

class RawMessage(Base):
    __tablename__ = 'raw_messages'

    id = Column(Integer, primary_key=True)

    message_name = Column(String(100))
    data = Column(PickleType)
